﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KCL.Code.Entities;
using KCL.Code.Business;
using KCL.Code.Data;

namespace KCL.Organization
{
    /// <summary>
    /// Summary description for Company
    /// </summary>
    public class CompanyHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // get parameters from query string
			HttpPostedFile logoFile = context.Request.Files.Count > 0 ? context.Request.Files[0] : null;
			var companyName = context.Request.Form["companyName"] != null ? context.Request.Form["companyName"].Trim().Replace("'", "''") : "";
			var address = context.Request.Form["address"] != null ? context.Request.Form["address"].Trim().Replace("'", "''") : "";
			var phone = context.Request.Form["phone"] != null ? context.Request.Form["phone"].Trim().Replace("'", "''") : "";
			var mobile = context.Request.Form["mobile"] != null ? context.Request.Form["mobile"].Trim().Replace("'", "''") : "";
			var fax = context.Request.Form["fax"] != null ? context.Request.Form["fax"].Trim().Replace("'", "''") : "";
			var accountNumber = context.Request.Form["accountNumber"] != null ? context.Request.Form["accountNumber"].Trim().Replace("'", "''") : "";
			var bank = context.Request.Form["bank"] != null ? context.Request.Form["bank"].Trim().Replace("'", "''") : "";
			var taxNumber = context.Request.Form["taxNumber"] != null ? context.Request.Form["taxNumber"].Trim().Replace("'", "''") : "";
			var director = context.Request.Form["director"] != null ? context.Request.Form["director"].Trim().Replace("'", "''") : "";

            context.Response.ContentType = "application/html";
            var responseData = string.Empty;

            // return empty if question or keyword is missing
            if (string.IsNullOrEmpty(companyName))
            {
                context.Response.Write("alert(\"Thông tin tên công ty không hợp lệ!\");");
                return;
            }

            KCL.Code.Entities.Company company = new Code.Entities.Company();            

            company.name = companyName;
            company.address = address;
            company.director = director;
            company.phone = phone;
            company.mobile = mobile;
            company.fax = fax;
            company.accountNumber = accountNumber;
            company.bank = bank;
            company.taxNumber = taxNumber;
			company.logo = logoFile != null ? context.Server.MapPath("../styles/images/" + logoFile.FileName) : null;

            try
            {
                CompanyDAO companyDAO = new CompanyDAO();
				if (logoFile != null) logoFile.SaveAs(company.logo);
				companyDAO.EditCompany(company);
				context.Response.Write("alert('Chỉnh sửa thành công.'); location.reload(true);");
			}
            catch (Exception ex)
            {
                context.Response.Write(string.Format("alert(\"Có lỗi: {0}\");", ex.Message));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}